package com.cl.edu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cl.core.orm.Page;
import com.cl.core.orm.SearchFilter;
import com.cl.core.orm.hibernate.HibernateWebUtils;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.dao.AreaDAO;
import com.cl.edu.dao.BookingstudentDAO;
import com.cl.edu.dao.BookingteacherDAO;
import com.cl.edu.dao.NewsDAO;
import com.cl.edu.dao.StudentDAO;
import com.cl.edu.entity.Area;
import com.cl.edu.entity.Bookingstudent;
import com.cl.edu.entity.Bookingteacher;
import com.cl.edu.entity.News;
import com.cl.edu.entity.Student;

/** 
 * 学生找家教信息
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class StudentManager {
	@Autowired
	private StudentDAO studentDAO;
	@Autowired
	private AreaDAO areaDAO;
	@Autowired
	private NewsDAO newsDAO;
	@Autowired
	private BookingteacherDAO bookingteacherDAO;
	@Autowired
	private BookingstudentDAO bookingstudentDAO;
	public Student getStudentById(Long id) {
		// TODO Auto-generated method stub
		return studentDAO.get(id);
	}

	/**
	 * 保存学生信息
	 * 
	 * @param stu
	 */
	public void save(Student stu) {
		studentDAO.save(stu);
	}
	/**
	 * 保存预约信息
	 * @param bt
	 */
	public void saveBookingMessage(Bookingteacher bt){
		bookingteacherDAO.save(bt);
	}
	/**
	 * 保存预约学员信息
	 * @param bt
	 */
	public void saveBookingStudentMessage(Bookingstudent bs){
		bookingstudentDAO.save(bs);
	}
	/**
	 * 查询沈阳市下级行政区划
	 * 
	 * @return
	 */
	public List<Area> getShenYang() {
		return areaDAO.getShenYang();
	}
	/**
	 * 分页查询找家教信息
	 * 
	 * @param
	 * @return
	 */
	public Page<Student> getChoiceList(Page<Student> page) {
		StringBuffer hql = new StringBuffer("from Student student");
		hql.append(" where student.state=0 ");
		List<SearchFilter> filters = HibernateWebUtils.buildSearchFilters(
				Struts2Utils.getRequest(), "filter_");
		for (SearchFilter filter : filters) {
			if ("subject".equals(filter.getPropertyName())&&!(filter.getPropertyValue()).equals("请输入求教学科")) {
				hql.append(" and student.subject like '%").append(
						filter.getPropertyValue()).append("%'");
			} else if ("sex".equals(filter.getPropertyName())&&!(filter.getPropertyValue()).equals("请选择学员性别")) {
				hql.append(" and student.sex='").append(
						filter.getPropertyValue()).append("'");
			} else if ("sexrequire".equals(filter.getPropertyName())&&!(filter.getPropertyValue()).equals("教员性别要求")) {
				hql.append(" and student.sexrequire ='").append(
						filter.getPropertyValue()).append("'");
			}else if ("id".equals(filter.getPropertyName())&&!(filter.getPropertyValue()).equals("定单编号")) {
				hql.append(" and student.id =").append(
						filter.getPropertyValue());
			}else if ("areaid".equals(filter.getPropertyName())&&!(filter.getPropertyValue()).equals("授课区域")) {
				hql.append(" and student.areaid ='").append(
						filter.getPropertyValue()).append("'");
			}
		}
		hql.append(" order by student.").append(page.getOrderBy()).append(" ")
				.append(page.getOrder());
		page = studentDAO.findPage(page, hql.toString());

		return page;
	}
	/**
	 * 通过栏目id获取前n条数据
	 * @param page
	 * @param id
	 * @param n
	 * @return
	 */
	public Page<News> getNewsById(Page<News> page,Long id,int n) {
		StringBuffer hql = new StringBuffer("from News model ");
		hql.append(" where model.channel.id = ").append(id);
		page.setPageSize(n);
		hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
		page = newsDAO.findPage(page, hql.toString());

		return page;
	}
}
